package com.sui.bigdata.rtcadmin.util;

import com.alibaba.fastjson.JSON;
import com.feidee.fdcommon.configuration.CustomConfiguration;
import com.google.common.collect.Lists;

import com.sui.bigdata.rtcadmin.constant.JobConstant;
import com.sui.bigdata.rtcadmin.dto.JobConfigDto;
import com.sui.bigdata.rtcadmin.exception.TableNotFoundException;
import com.sui.bigdata.rtcadmin.repository.mapper.MetaMapper;
import com.sui.bigdata.rtcadmin.repository.model.Meta;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author YongChen
 * @date 2019/11/15 16:28
 * @description
 * @email yong_chen@sui.com
 */
@Component
public class MetaUtils {

    @Autowired
    private MetaMapper metaMapper;

    /*public List<CreateTableParser.SqlParserResult> getMetaTables(JobConfigDto jobConfigDto){
        List<String> execSqlTables = SqlParser.getExecSqlTables(jobConfigDto.getExecSql());
        List<CreateTableParser.SqlParserResult> metaTables = Lists.newArrayList();
        for (String execSqlTable : execSqlTables) {
            CreateTableParser.SqlParserResult sqlParserResult = new CreateTableParser.SqlParserResult();
            Meta meta = metaMapper.queryByTableName(execSqlTable);
            if (meta == null){
                throw new TableNotFoundException(execSqlTable);
            }
            sqlParserResult.setTableName(meta.getTableName());
            sqlParserResult.setFieldsInfoStr(meta.getFieldInfo());
            Map<String, Object> propMap = JSON.parseObject(meta.getProp(), Map.class);
            propMap.put("type",meta.getComponent());
            sqlParserResult.setPropMap(propMap);
            metaTables.add(sqlParserResult);
        }
        return metaTables;
    }*/

    public String listComponent(String type){
        switch (type){
            case "source" :
                return CustomConfiguration.getString(JobConstant.SQL_SOURCE_COMPONENT);
            case "side" :
                return CustomConfiguration.getString(JobConstant.SQL_SIDE_COMPONENT);
            case "sink" :
                return CustomConfiguration.getString(JobConstant.SQL_SINK_COMPONENT);
            default:
                break;
        }
        return null;
    }
}
